﻿using Power.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Media;
using System.Reflection;
using System.IO;

namespace Power.Common
{
    public static class Common
    {
        static AppHost appHost = null;
        public static void InitWebServer(int port)
        {
            if (appHost == null)
            {
                appHost = new AppHost();

                appHost.Init();
                appHost.Start(string.Format("http://*:{0}/", port));
            }
        }
        /// <summary>  
        /// 获得指定元素的所有子元素  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="obj"></param>  
        /// <param layer="是否递归查找">-1是 </param>  
        /// <returns></returns>  
        public static List<T> GetChildObjects<T>(DependencyObject obj,int layer = -1) where T : FrameworkElement
        {
            DependencyObject child = null;
            List<T> childList = new List<T>();
            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);

                if (child is T)
                {
                    childList.Add((T)child);
                }
                if (layer == -1)
                {
                    childList.AddRange(GetChildObjects<T>(child));
                }
            }
            return childList;
        }

        /// <summary>   
        /// 获取父可视对象中第一个指定类型的子可视对象   
        /// </summary>   
        /// <typeparam name="T">可视对象类型</typeparam>   
        /// <param name="parent">父可视对象</param>   
        /// <returns>第一个指定类型的子可视对象</returns>   
        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }

        /// <summary>   
        /// 获取DataGrid控件单元格   
        /// </summary>   
        /// <param name="dataGrid">DataGrid控件</param>   
        /// <param name="rowIndex">单元格所在的行号</param>   
        /// <param name="columnIndex">单元格所在的列号</param>   
        /// <returns>指定的单元格</returns>   
        public static DataGridCell GetCell(this DataGrid dataGrid, int rowIndex, int columnIndex)
        {
            DataGridRow rowContainer = dataGrid.GetRow(rowIndex);
            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                if (cell == null)
                {
                    dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                }
                return cell;
            }
            return null;
        }
        /// <summary>   
        /// 获取DataGrid的行   
        /// </summary>   
        /// <param name="dataGrid">DataGrid控件</param>   
        /// <param name="rowIndex">DataGrid行号</param>   
        /// <returns>指定的行号</returns>   
        public static DataGridRow GetRow(this DataGrid dataGrid, int rowIndex)
        {
            DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
            if (rowContainer == null)
            {
                dataGrid.UpdateLayout();
                dataGrid.ScrollIntoView(dataGrid.Items[rowIndex]);
                rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
            }
            return rowContainer;
        }

        /// <summary>
        /// 将图片以二进制流
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static byte[] GetImagesBytes(String path)
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
            BinaryReader br = new BinaryReader(fs);
            return br.ReadBytes((int)fs.Length);
        }
            
        /// <summary>
        /// 名称
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetNameList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "名称1", SelectID = "0" },
                    new SelectListItem { SelectItem = "名称2", SelectID = "1" }, new SelectListItem { SelectItem = "名称3", SelectID = "2" },
                    new SelectListItem { SelectItem = "名称4", SelectID = "3" }};
        }

        /// <summary>
        /// 类型
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetTypeList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "类型1", SelectID = "0" },
                    new SelectListItem { SelectItem = "类型2", SelectID = "1" }, new SelectListItem { SelectItem = "类型3", SelectID = "2" },
                    new SelectListItem { SelectItem = "类型4", SelectID = "3" }};
        }

        /// <summary>
        /// 等级
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetLevelList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "等级1", SelectID = "0" },
                    new SelectListItem { SelectItem = "等级2", SelectID = "1" }, new SelectListItem { SelectItem = "等级3", SelectID = "2" },
                    new SelectListItem { SelectItem = "等级4", SelectID = "3" }};
        }

        /// <summary>
        /// 片区
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetAreaSelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "所有片区", SelectID = "0" },
                    new SelectListItem { SelectItem = "所有片区1", SelectID = "1" }, new SelectListItem { SelectItem = "所有片区2", SelectID = "2" },
                    new SelectListItem { SelectItem = "所有片区3", SelectID = "3" } };
        }

        /// <summary>
        /// 区块
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetBlockSelectList() {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "区块", SelectID = "0" },
                        new SelectListItem { SelectItem = "区块1", SelectID = "1" }, new SelectListItem { SelectItem = "区块2", SelectID = "2" },
                        new SelectListItem { SelectItem = "区块3", SelectID = "3" } };
        }

        /// <summary>
        /// 工点
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetWorkSelectList() {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "工点", SelectID = "0" },
                    new SelectListItem { SelectItem = "工点1", SelectID = "1" }, new SelectListItem { SelectItem = "工点2", SelectID = "2" },
                    new SelectListItem { SelectItem = "工点3", SelectID = "3" } };
        }

        /// <summary>
        /// 批次
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetBatchSelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "批次", SelectID = "0" },
                    new SelectListItem { SelectItem = "批次1", SelectID = "1" }, new SelectListItem { SelectItem = "批次2", SelectID = "2" },
                    new SelectListItem { SelectItem = "批次3", SelectID = "3" } };
        }

        /// <summary>
        /// 批次
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetBatchSelectList2()
        {
            return new List<SelectListItem>() {
                    new SelectListItem { SelectItem = "05", SelectID = "1" }, new SelectListItem { SelectItem = "06", SelectID = "2" },
                    new SelectListItem { SelectItem = "07", SelectID = "3" } };
        }

        /// <summary>
        /// 管道
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetPipSelectList() {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "管道", SelectID = "0" },
                    new SelectListItem { SelectItem = "管道1", SelectID = "1" }, new SelectListItem { SelectItem = "管道2", SelectID = "2" },
                    new SelectListItem { SelectItem = "管道3", SelectID = "3" } };
        }

        /// <summary>
        /// 缺陷类型
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetDefectTypeSelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "缺陷类型", SelectID = "0" },
                    new SelectListItem { SelectItem = "缺陷类型1", SelectID = "1" }, new SelectListItem { SelectItem = "缺陷类型2", SelectID = "2" },
                    new SelectListItem { SelectItem = "缺陷类型3", SelectID = "3" } };
        }

        /// <summary>
        /// 缺陷等级
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetDefectLevelSelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "缺陷等级", SelectID = "0" },
                    new SelectListItem { SelectItem = "缺陷等级1", SelectID = "1" }, new SelectListItem { SelectItem = "缺陷等级2", SelectID = "2" },
                    new SelectListItem { SelectItem = "缺陷等级3", SelectID = "3" } };
        }

        /// <summary>
        /// 项目
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetProSelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "请选择项目信息", SelectID = "0" },
                    new SelectListItem { SelectItem = "项目1", SelectID = "1" }, new SelectListItem { SelectItem = "项目2", SelectID = "2" },
                    new SelectListItem { SelectItem = "项目3", SelectID = "3" } };
        }

        /// <summary>
        /// 角色
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetRoleSelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "请选择角色信息", SelectID = "0" },
                    new SelectListItem { SelectItem = "角色1", SelectID = "1" }, new SelectListItem { SelectItem = "角色2", SelectID = "2" },
                    new SelectListItem { SelectItem = "角色3", SelectID = "3" } };
        }

        /// <summary>
        /// 报表类型
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetReportList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "综合表", SelectID = "0" },
                    new SelectListItem { SelectItem = "成果表", SelectID = "1" }, new SelectListItem { SelectItem = "维护管段表", SelectID = "2" },
                    new SelectListItem { SelectItem = "检查井检查情况汇总表", SelectID = "3" },new SelectListItem { SelectItem = "分类工作量表", SelectID = "4" },
            new SelectListItem { SelectItem = "工点工作量表", SelectID = "5" },new SelectListItem { SelectItem = "管段工作量表", SelectID = "6" },
            new SelectListItem { SelectItem = "缺陷种类表", SelectID = "7" },new SelectListItem { SelectItem = "缺陷种类图", SelectID = "8" },
            new SelectListItem { SelectItem = "缺陷等级表", SelectID = "9" },new SelectListItem { SelectItem = "缺陷等级图", SelectID = "10" },
            new SelectListItem { SelectItem = "缺陷管段表", SelectID = "11" },new SelectListItem { SelectItem = "缺陷密度图", SelectID = "12" }};
        }

        /// <summary>
        /// 获取省份
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetProvinceList()
        {
            return new List<SelectListItem>() { 
                new SelectListItem { SelectItem = "省份1", SelectID = "1" },
                new SelectListItem { SelectItem = "省份2", SelectID = "2" },
                new SelectListItem { SelectItem = "省份3", SelectID = "3" },
                new SelectListItem { SelectItem = "省份4", SelectID = "4" },
                new SelectListItem { SelectItem = "省份5", SelectID = "5" },

            };
        }

        /// <summary>
        /// 获取城市
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetCityList()
        {
            return new List<SelectListItem>() {
                new SelectListItem { SelectItem = "城市1", SelectID = "1" },
                new SelectListItem { SelectItem = "城市2", SelectID = "2" },
                new SelectListItem { SelectItem = "城市3", SelectID = "3" },
                new SelectListItem { SelectItem = "城市4", SelectID = "4" },
                new SelectListItem { SelectItem = "城市5", SelectID = "5" },

            };
        }
        /// <summary>
        /// 模板
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetMotherboadrSelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "模板", SelectID = "0" },
                    new SelectListItem { SelectItem = "模板1", SelectID = "1" }, new SelectListItem { SelectItem = "模板2", SelectID = "2" },
                    new SelectListItem { SelectItem = "模板3", SelectID = "3" } };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetSelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "0", SelectID = "0" },
                    new SelectListItem { SelectItem = "1", SelectID = "1" }, new SelectListItem { SelectItem = "2", SelectID = "2" },
                    new SelectListItem { SelectItem = "3", SelectID = "3" } };
        }

        /// <summary>
        /// 缺陷类别
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetDefectCategorySelectList()
        {
            List<SelectListItem> resultList = new List<SelectListItem>() {
                new SelectListItem(){SelectID = "结构性", SelectItem = "结构性" },
                new SelectListItem(){SelectID = "功能性", SelectItem = "功能性" }
            };
            return resultList;
        }
        public static List<SelectListItem> GetDefectNameSelectList()
        {
            List<SelectListItem> resultList = new List<SelectListItem>() {
                new SelectListItem(){SelectID = "", SelectItem = "全部" },
                new SelectListItem(){SelectID = "PL", SelectItem = "破裂" },
                new SelectListItem(){SelectID = "BX", SelectItem = "变形" },
                new SelectListItem(){SelectID = "FS", SelectItem = "腐蚀" },
                new SelectListItem(){SelectID = "CK", SelectItem = "错口" },
                new SelectListItem(){SelectID = "QF", SelectItem = "起伏" },
                new SelectListItem(){SelectID = "TJ", SelectItem = "脱节" },
                new SelectListItem(){SelectID = "TL", SelectItem = "接口材料脱落" },
                new SelectListItem(){SelectID = "AJ", SelectItem = "支管暗接" },
                new SelectListItem(){SelectID = "CR", SelectItem = "异物穿入" },
                new SelectListItem(){SelectID = "SL", SelectItem = "渗透" },
                new SelectListItem(){SelectID = "CJ", SelectItem = "沉积" },
                new SelectListItem(){SelectID = "JG", SelectItem = "结垢" },
                new SelectListItem(){SelectID = "ZW", SelectItem = "障碍物" },
                new SelectListItem(){SelectID = "CQ", SelectItem = "残墙,坝根" },
                new SelectListItem(){SelectID = "SG", SelectItem = "树根" },
                new SelectListItem(){SelectID = "FZ", SelectItem = "浮渣" },
            };
            return resultList;
        }

        /// <summary>
        /// 缺陷等级
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetDefectLevel1SelectList()
        {
            return new List<SelectListItem>() {
                    new SelectListItem { SelectID = "0", SelectItem = "全部" },
                    new SelectListItem { SelectItem = "1", SelectID = "1" },
                    new SelectListItem { SelectItem = "2", SelectID = "2" },
                    new SelectListItem { SelectItem = "3", SelectID = "3" },
                    new SelectListItem { SelectItem = "4", SelectID = "4" } };
        }

        /// <summary>
        /// AI识别缺陷类别
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetAIDefectCategorySelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "AI识别缺陷类别", SelectID = "0" },
                    new SelectListItem { SelectItem = "AI识别缺陷类别1", SelectID = "1" }, new SelectListItem { SelectItem = "AI识别缺陷类别2", SelectID = "2" },
                    new SelectListItem { SelectItem = "AI识别缺陷类别3", SelectID = "3" } };
        }

        /// <summary>
        /// AI识别缺陷等级
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetAIDefectLevelSelectList()
        {
            return new List<SelectListItem>() { new SelectListItem { SelectItem = "AI识别缺陷等级", SelectID = "0" },
                    new SelectListItem { SelectItem = "AI识别缺陷等级1", SelectID = "1" }, new SelectListItem { SelectItem = "AI识别缺陷等级2", SelectID = "2" },
                    new SelectListItem { SelectItem = "AI识别缺陷等级3", SelectID = "3" } };
        }
    }
}
